/*********************************************************
*
* 文件名：MQCP.H
* File name: MQCP.H 
*
* 协议名称：消息队列控制协议（Message queue control protocol，简写为MQCP协议）
* protocol name: message queue control protocol (Message queue control protocol, abbreviated to MQCP protocol)
*
* 说明：	MQCP协议是为满足小数据量、实时连接和通讯的需求，为适应于不同性能的MCU而开发的轻量级通讯协议。
* 			本文件用于MQCP协议的数据定义，结构体定义等。
* description: 	MQCP protocol is a lightweight communication protocol developed to meet the requirements
*				of small amount of data,real-time connection and communication, and to adapt to different
*				performance of MCU.
*				This document is used for data definition of MQCP protocol, structure definition, etc.
*
*********************************************************/

#ifndef __MQCP_H__
#define __MQCP_H__

/*	BEGIN Includes	*/

#include "allinclude.h"
#include "alldefine.h"

#include "Decrypt.h"

#include "ModbusSlave.h"
#include "SysUtcTime.h"

#include "SysTick.h"
#include "SysUart.h"

#include "TimeList.h"

/*	END Includes	*/


/*	BEGIN DEFINE	*/

#define		MQCP_SIZE_MAX				512

#define		MQCP_LENGTH_SIZE		11
#define 	MQCP_TRY_OUT				3					//数据发送超时次数
#define		MQCP_TIME_OUT				10*1000		//数据发送超时时间

#define 	MQCP_HEART_OUT			60*1000

#define 	MQCP_GET_TIME_OUT		3600			//校时时间

#define		MQCP_LOGIN_TIME_OUT	60*10*1000  //未收到服务器数据时间

#define		MQCP_UPDATA					0xef
#define		MQCP_DOWN						0xfe

//设备请求登陆
#define 	MQCP_Cmd_Login				0x7f
#define 	MQCP_Cmd_Login_Rcv		0xff

//设备请求心跳
#define 	MQCP_Cmd_Heart				0x7e
#define 	MQCP_Cmd_Heart_Rcv		0xfe

//设备请求当前时间
#define		MQCP_Cmd_GetTime			0x7d
#define		MQCP_Cmd_GetTime_Rcv	0xfd

//服务器请求定时任务
#define		MQCP_Cmd_TimeList			0x7c
#define		MQCP_Cmd_TimeList_Rcv	0xfc

//上报ICCID
#define		MQCP_Cmd_ICCID			0x7b
#define		MQCP_Cmd_ICCID_Rcv	0xfb

//设备上报数据
#define 	MQCP_Cmd_Send					0x01
#define 	MQCP_Cmd_Send_Rcv			0x81

//服务器下发消息
#define		MQCP_Cmd_Rcv					0x02
#define		MQCP_Cmd_Rcv_Rcv			0x82







#define		MQCP_Version				2


typedef enum
{
	MQCP_INIT = 0,
	MQCP_LOGIN,
	MQCP_LOGIN_WAIT,
	MQCP_GET_TIME,
	MQCP_GET_TIEM_WAIT,
	MQCP_ONLINE,
	MQCP_HEART,
	MQCP_SEND,
	MQCP_RCV,
	MQCP_HEART_WAIT,
	MQCP_OFFLINE,
}MQCP_STEP;

#define		MQCP_BUSY						0

/*	END DEFINE	*/


/*	BEGIN typedef	*/


/*	Mqcp 设备信息结构体	*/
/* Mqcp Device Information structure */

typedef struct
{
	uint32_t	DevId;					//40193
	uint32_t	DevSn;					//40195
	uint8_t 	DevKey[16];			//40197~40204
	
}MqcpDeviceDef;

/*	Mqcp 消息结构体	*/
/* Mqcp message structure */

typedef struct
{
	uint8_t		Head;
	
	uint8_t 	Length_H;
	uint8_t 	Length_L;
	
	uint8_t 	Ver;
	uint8_t 	Numer;
	
	uint8_t 	Id_H;
	uint8_t 	Id_U;
	uint8_t 	Id_D;
	uint8_t 	Id_L;
	
	uint8_t 	Cmd;
	
	uint8_t		Data[MQCP_SIZE_MAX+1];
	
}MqcpMessageDef;

typedef struct
{
	MQCP_STEP	Mqcp_step;		//mqcp流程
	uint8_t		Mqcp_Try_Num;	//当前流程重发次数
	
	uint32_t		Mqcp_Tick;	//超时时间
	uint32_t		GetTime;
	
	uint8_t rcvbuf[sizeof(sTimeListWorkDef)+5];
	
	MqcpDeviceDef  Mqcp_Device;
	
	MqcpMessageDef Mqcp_Message;
	MqcpMessageDef	Mqcp_ReciveMessage;
	
	tUartRxBufDef	*net_buf;
}MqcpDataDef;


/*	END typedef	*/


/*	BEGIN extern variables	*/

//extern uint8_t		Mqcp_step;

/*	END extern variables	*/


/*	BEGIN extern function	*/

//extern MqcpDeviceDef	Mqcp_Device;


//extern uint16_t	AutoResetNET;


extern void MqcpProcess(void);
extern void ModbusProcess(void);
/*	END extern function	*/


#endif




